package com.spmsys.filter;

import com.alibaba.fastjson.JSONObject;
import com.spmsys.logUtil.LOGGER;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

@WebFilter(filterName = "InitFilter", urlPatterns = "/controller/*")
public class B_InitFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, @NotNull FilterChain filterChain) throws IOException {
        try {
            System.out.println("InitFilter");
            //包装
            ContentCachingRequestWrapper httpServletRequest = new ContentCachingRequestWrapper((HttpServletRequest) servletRequest);
            ContentCachingResponseWrapper httpServletResponse = new ContentCachingResponseWrapper((HttpServletResponse) servletResponse);
            //获取请求ID
            String ID = httpServletRequest.getRequestId();
            //获取请求日志
            String requestLog = LOGGER.request_log(ID, httpServletRequest);
            System.out.println("BEGIN:" + ID + "\n");
            System.out.println(requestLog);
            System.out.println("END\n");
            //放行
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            //获取响应日志
            String responseLog = LOGGER.response_log(ID, httpServletResponse);
            System.out.println("BEGIN:" + ID + "\n");
            System.out.println(responseLog);
            System.out.println("END\n");
            //写入日志
            LOGGER.record_log("Request:\n" + requestLog + "\n" + "Response:\n" + responseLog + "\n");
            //数据写回
            httpServletResponse.copyBodyToResponse();
        } catch (Exception e) {
            HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
            //获取当前时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            //设置字符集、接受所有跨域请求、设置时间
            httpServletResponse.setContentType("application/json;charset=utf-8");
            httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
            httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
            httpServletResponse.setHeader("Access-Control-Allow-Headers", "*");
            httpServletResponse.setHeader("Date", simpleDateFormat.format(Calendar.getInstance().getTime()));
            JSONObject responseData = new JSONObject();
            responseData.put("errorCode", "INIT_FILTER_FAIL");
            responseData.put("errorMessage", e.getMessage());
            PrintWriter printWriter = httpServletResponse.getWriter();
            printWriter.println(responseData);
            printWriter.close();
            e.printStackTrace();
        }
    }
}
